<template>
  <transition>
    <div
      v-show="visible"
      class="fix-header-wrapper"
    >
      <div class="header-wrapper">
        <div class="logo">
          <router-link
            :to="{path:'/'}"
          >
            小锅锅
          </router-link>
          <sup>Blog</sup>
        </div>
        <div class="nav">
          <ul class="nav-menu">
            <li
              v-for="(item, index) in navList"
              :key="index"
              class="item"
            >
              <router-link
                :to="{path:item.path}"
              >
                <span>{{ item.title }}</span>
              </router-link>
            </li>
          </ul>
          <div class="nav-menu-mini">
            <el-dropdown
              trigger="click"
              @command="handleCommand"
            >
              <div class="el-dropdown-link">
                {{ title }}
                <el-icon>
                  <ArrowDown />
                </el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item
                    v-for="(item, index) in navList"
                    :key="index"
                    :command="item.title"
                  >
                    <router-link
                      :to="{path:item.path}"
                    >
                      {{ item.title }}
                    </router-link>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
        <div class="search">
          <el-autocomplete
            v-model="keyWord"
            :fetch-suggestions="querySearchAsync"
            value-key="title"
            suffix-icon="Search"

            size="default"
            placeholder="搜索本站"
            @select="handleSelect"
          />
        </div>
        <LoginCpt />
      </div>
    </div>
  </transition>
</template>

<script lang="ts" scoped>
import LoginCpt from '@/components/Login/index.vue'

import { defineComponent, reactive, toRefs, onMounted, onUnmounted, inject } from 'vue'
import { userStore } from '@/store/user'
import { InitData } from '@/types/nav'
import { useRouter } from 'vue-router'

export default defineComponent({
  name: 'LyHeader',
  components: {
    LoginCpt
  },
  setup () {
    const $get:any = inject('$get')

    const data = reactive(new InitData())

    const store = userStore()
    const router = useRouter()

    // 页面加载时
    onMounted(() => {
      window.addEventListener('scroll', headershow)
    })

    onUnmounted(() => {
      window.removeEventListener('scroll', headershow)
    })

    const register = () => {}
    const login = () => {}
    const logout = () => {
      store.setToken(null)
    }

    const handleCommand = (x:string) => {
      data.title = x
    }
    const handleSelect = (res:any) => {
      const id = res.id
      if (router.currentRoute.value.path !== '/article/' + id) {
        router.push('/article/' + id)
      }
    }
    const querySearchAsync = async (keyWord:string, cb:any) => {
      if (keyWord) {
        const params = {
          nowPage: 1,
          pageSize: 20,
          keyWord
        }
        const list = await $get('article/keyWord_list', params
        )
        cb(list.rows)
      } else {
        // eslint-disable-next-line n/no-callback-literal
        cb([])
      }
    }
    const headershow = () => {
      // 头部高度为70px
      const height = 70
      const offsetTop = window.pageYOffset || document.documentElement.scrollTop
      data.visible = offsetTop < height
    }

    return {
      ...toRefs(data),
      register,
      login,
      logout,
      handleCommand,
      handleSelect,
      querySearchAsync
    }
  }
})
</script>

<style lang="scss" scope>

@import '@/assets/css/constant.scss';

.v-enter-active {
  transition: all 0.5s ease;
}
.v-leave-active {
  transition: all 0.5s ease;
}
.v-enter,
.v-leave-to {
  transform: translateY(-70px);
}

.el-dropdown {
  line-height: inherit !important;
}

@media screen and (max-width: 540px) {
  .header {
    height: 50px;
    line-height: 50px;
  }
  .search {
    width: 30%;
  }
  .logo h2 {
    font-size: 18px;
  }
  .logo h2 sup {
    font-size: 10px;
  }
}
.dark {
  .fix-header-wrapper {
    background: $theme-color3;
  }
}
.fix-header-wrapper {
  position: fixed;
  top: 0;
  z-index: 100;
  width: 100%;
  border-bottom: 1px solid $theme-color2;
  background: $theme-color6;
  .header-wrapper {
    display: flex;
    justify-content: space-between;
    margin: 0 auto;
    height: 70px;
    line-height: 70px;
    transition: all 0.2s;
    .logo {
      font-size: 20px;
      sup {
        font-size: 13px;
      }
    }
    .nav {
      ul li {
        position: relative;
        display: inline-block;
        cursor: pointer;
      }
      ul li:hover {
        color: $theme-color1;
      }
      .nav-menu {
        margin: 0;
        padding: 0;
        .item {
          margin: 0 20px;
          user-select: none;
        }
        .item:hover:before {
          left: 0;
          width: 100%;
        }
        .item:before {
          position: absolute;
          bottom: 10px;
          left: 50%;
          width: 0;
          height: 2px;
          background-color: $theme-color1;
          content: '';
          transition: all 0.3s ease;
        }
      }
      .nav-menu-mini {
        display: none;
        text-align: center;
      }
    }
  }
}
</style>
